package com.cy.personer.web;

import com.cy.personer.bean.FirstMenu;
import com.cy.personer.bean.Role;
import com.cy.personer.bean.SecondMenu;
import com.cy.personer.biz.PowerBiz;
import com.cy.personer.biz.impl.PowerBizImpl;
import com.cy.personer.mapper.PowerMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/power")
public class PowerHandler {
    private Map<String,Object> map;
    private List<Integer> secondMenuId;
    private PowerBiz powerBiz =new PowerBizImpl();
    @Resource
    private  PowerBizImpl powerBizImpl;

@RequestMapping("power.action")
//查询角色
  public  String powerlist(HttpServletRequest request, Integer pageNum){
    if (pageNum == null||pageNum==0) {
        pageNum = 1;
    }
    PageInfo<Role> roleList=powerBizImpl.getRoles(pageNum);
          request.setAttribute("roleList",roleList);
          return "admin/power";
  }
@RequestMapping("list.action")
/**
 * 权限分配
 * @author whl
 */
public  String allot(HttpServletRequest request,Integer roleId){
      List<FirstMenu>firstMenuList= powerBizImpl.firstList();
      request.setAttribute("firstMenuList",firstMenuList);
      List<SecondMenu>secondMenuList=powerBizImpl.secondList();
      request.setAttribute("secondMenuList",secondMenuList);
      List<SecondMenu>secondMenu=powerBizImpl.queryList(roleId);
      request.setAttribute("secondMenu",secondMenu);
      request.setAttribute("roleId",roleId);
         return "admin/power_up";
    }
/**
 * 权限修改
 * @author whl
 */
   @RequestMapping(value = ("up.action")
   ,method= RequestMethod.POST, produces="application/json;charset=utf-8")
   public  @ResponseBody String  up( int roleId,@RequestParam(value = "secondMenuId[]") Integer[]secondMenuId){
      String msg=null;
   boolean flag =powerBizImpl.add(roleId, secondMenuId);
   if (flag){
       msg="true";
   }else{
       msg="false";
   }
       return msg;
    }


    public PowerBizImpl getPowerBizImpl() {
        return powerBizImpl;
    }

    public void setPowerBizImpl(PowerBizImpl powerBizImpl) {
        this.powerBizImpl = powerBizImpl;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public List<Integer> getSecondMenuId() {
        return secondMenuId;
    }

    public void setSecondMenuId(List<Integer> secondMenuId) {
        this.secondMenuId = secondMenuId;
    }
}



